/**
  ******************************************************************************
  * @file    printf.h
  * @author  chengbb
  * @version V1.0
  * @date    2017-07-06
  * @brief   标准输入输出配置
  ******************************************************************************
  * @attention
  *
  * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
  * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
  * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
  * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
  * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  *
  * <h2><center>&copy; COPYRIGHT 2016 MindX</center></h2>
  ******************************************************************************
  */
/** @addtogroup BSP
* @{
*/
#include "printf.h"
#include <stdio.h>
#include <stdarg.h>
#include "bsp.h"
#include "log.h"

#pragma import(__use_no_semihosting_swi)

/** Retargets the C library printf function to the USART. */
struct __FILE
{
    int handle;  /**< handler */
};
/** Retargets the C library printf function to the USART. */
FILE __stdout;
/** Retargets the C library printf function to the USART. */
FILE __stdin;

/**
  ******************************************************************************
  * @brief  串口发送一个字符
  * @param  USARTx：哪一个串口发送
  * @retval _ulCh：返回发送的值
  ******************************************************************************/
static uint32_t UsartXSendChar(USART_TypeDef* USARTx, uint32_t _ulCh)
{
    /* Write a character to the USART */
    USART_SendData(USARTx, (uint8_t)_ulCh);

    /* Loop until the end of transmission */
    while(USART_GetFlagStatus(USARTx, USART_FLAG_TXE) == RESET)
    {
    }
    return _ulCh;
}

/**
  ******************************************************************************
  * @brief  串口发送多个字符
  * @param  _ucpcStr：发送的字符数组
  * @param  _ulLength：发送数组的长度
  * @retval _ulLength：发送的字符数
  ******************************************************************************/
uint32_t UsartXSendString(USART_TypeDef* USARTx, uint8_t* _ucpStr, uint32_t _ulLength)
{
    uint32_t i = 0;

    while(i < _ulLength)
    {
        UsartXSendChar(USARTx, _ucpStr[i++]);
    }
    return _ulLength;
}

/**
  ******************************************************************************
  * @brief  Retargets the C library printf function to the USART.
  * @param  ch  字符
  * @param   *f 文件
  * @retval 字符
  ******************************************************************************/
int fputc(int ch, FILE *f)
{
    return (UsartXSendChar(CONSOLE_USART, ch));
}

/**
  ******************************************************************************
  * @brief  Retargets the C library printf function to the USART.
  * @param  ch 1个字符
  * @retval 字符
  ******************************************************************************/
void _ttywrch(int ch)
{
    UsartXSendChar(CONSOLE_USART, ch);
}

/**
  ******************************************************************************
  * @brief  Retargets the C library printf function to the USART.
  * @param  return_code 错误代码
  * @retval None.
  ******************************************************************************/
void _sys_exit(int return_code)
{
    while(1)
    {
    }
}

/**
  * @}
  */
/******************* (C) COPYRIGHT 2017 MINDX ******END OF FILE******************/


